package com.quanshu.shop.main.adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.bumptech.glide.Glide;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.config.Config;
import com.quanshu.shop.base.tool.BaseTool;
import com.quanshu.shop.goods.view.GoodsWebviewActivity;
import com.quanshu.shop.main.entity.ShopMainEntity;

import java.util.List;

/**
 * 作者  李哲
 * 创建时间 2018/7/18
 * 邮箱 lizhetongliao@163.com
 * 说明
 **/
public class SpecialAdapter extends DelegateAdapter.Adapter<SpecialAdapter.SpecialAdapterViewHolder> {
    private Context mContext;
    private List<ShopMainEntity.SpecialEntity> specialEntityList;
    private LayoutHelper layoutHelper;
    public static final int SPECIAL_TYPE = 4;

    public SpecialAdapter(Context mContext, List<ShopMainEntity.SpecialEntity> specialEntityList, LayoutHelper layoutHelper) {
        this.mContext = mContext;
        this.specialEntityList = specialEntityList;
        this.layoutHelper = layoutHelper;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return layoutHelper;
    }

    @NonNull
    @Override
    public SpecialAdapterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == SPECIAL_TYPE) {
            return new SpecialAdapterViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_shop_special_goods, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull SpecialAdapterViewHolder holder, int position) {
        Glide.with(mContext).load(Config.URL_HOST_SHOP + specialEntityList.get(position).getMob_headpic()).into(holder.imageView);
        holder.textView1.setText(specialEntityList.get(position).getSpecial_name());
        holder.textView2.setText("三安产品");
        holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle=new Bundle();
                bundle.putString(GoodsWebviewActivity.GOODS_WEBVIEW_URL,specialEntityList.get(position).getMob_link());
                BaseTool.goActivity(mContext, GoodsWebviewActivity.class,bundle);
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        return SPECIAL_TYPE;
    }

    @Override
    public int getItemCount() {
        return specialEntityList != null ? 1 : 0;
    }

    class SpecialAdapterViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView textView1;
        TextView textView2;
        RelativeLayout relativeLayout;

        public SpecialAdapterViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.shop_main_special_img);
            textView1 = itemView.findViewById(R.id.shop_main_special_txt);
            textView2 = itemView.findViewById(R.id.shop_main_special_txt1);
            relativeLayout = itemView.findViewById(R.id.specil1);
        }
    }
}
